package xyz.bali16.module.netdisk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.enums.ApiEnum;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.module.netdisk.entity.FileFolder;
import xyz.bali16.module.netdisk.entity.NetdiskPort;
import xyz.bali16.module.netdisk.model.filefolder.FileFolderAddParam;
import xyz.bali16.module.netdisk.model.filefolder.FileFolderPageParam;
import xyz.bali16.module.netdisk.model.filefolder.FileFolderUpdateParam;
import xyz.bali16.module.netdisk.model.netdiskport.NetdiskPortAddParam;
import xyz.bali16.module.netdisk.model.netdiskport.NetdiskPortPageParam;
import xyz.bali16.module.netdisk.model.netdiskport.NetdiskPortUpdateParam;
import xyz.bali16.module.netdisk.service.NetdiskPortService;
import xyz.bali16.module.netdisk.service.NetdiskService;

import javax.validation.Valid;
import java.util.List;

import static xyz.bali16.application.core.model.Result.*;

/**
 * 考虑安全性问题 这里只负责管理服务口占用情况
 * 当服务删除并不会影响服务先前所占有的文件/文件夹/文件仓库，需使用者手动按需删除
 */

@RestController
@RequestMapping(BaseController.API+"port")
public class NetdiskPortController extends NetdiskBaseController {
    @Autowired
    private NetdiskService netdiskService;
    @Autowired
    private NetdiskPortService netdiskPortService;

    @GetMapping
    @ApiOperation("服务端口列表数据")
    //@PreAuthorize("@auth.permission('plugin:netdisk:port:query')")
    public Result<IPage<NetdiskPort>> list(NetdiskPortPageParam param) {
        IPage<NetdiskPort> netdiskPortIPage = netdiskPortService.getPage(param);
        return success(ApiEnum.GET_SUCCESS, netdiskPortIPage);
    }

    @PostMapping
    @ApiOperation("添加服务端口")
    //@PreAuthorize("@auth.permission('plugin:netdisk:port:add')")
    public Result<String> add(@RequestBody @Valid NetdiskPortAddParam param) {
        try {
            netdiskService.createService(param.getName(),param.getPort());
            return response(ApiEnum.ADD_SUCCESS);
        } catch (Exception e) {
            return failure(ApiEnum.ADD_ERROR, "新增服务端口失败", e);
        }
    }

    @PutMapping
    @ApiOperation("更新服务端口")
    //@PreAuthorize("@auth.permission('plugin:netdisk:port:update')")
    public Result<String> update(@RequestBody @Valid NetdiskPortUpdateParam param) {
        try {
            netdiskPortService.update(param);
            return response(ApiEnum.UPDATE_SUCCESS);
        } catch (Exception e) {
            return failure(ApiEnum.UPDATE_ERROR, "更新服务端口失败", e);
        }
    }

    @DeleteMapping("/{netdiskPortId}")
    @ApiOperation("删除服务端口")
    @ApiImplicitParam(name = "netdiskPortId", value = "文件服务端口id", paramType = "path", required = true)
    //@PreAuthorize("@auth.permission('plugin:netdisk:port:delete')")
    public Result<String> delete(@PathVariable("netdiskPortId") String netdiskPortId) {
        try {
            netdiskPortService.delete(netdiskPortId);
            return response(ApiEnum.DELETE_SUCCESS);
        } catch (Exception e) {
            return failure(ApiEnum.DELETE_ERROR, e);
        }
    }
}
